package com.dingdayu.club.admin.api;

import android.util.Log;

import com.dingdayu.club.admin.common.util.HttpUtils;
import com.dingdayu.club.admin.common.util.SharePreferenceUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * Created by 大小姐 on 2015/10/24.
 */
public class GetMemberList {
    public static String CLUB_MEMBER_LIST_FILE = "member_list";
    public static SharePreferenceUtil spU;
    public static HashMap<String,String> hm;
    public static JSONObject jsonObj;
    public static int code;

    public GetMemberList(){
        spU = new SharePreferenceUtil(LoginApi.USER_INFO_FILE);
    }

    public static String[][] get(){
        String t = getCache();
        if(t == ""){
            HashMap<String, String> params = new HashMap<String, String>();
            spU = new SharePreferenceUtil(LoginApi.USER_INFO_FILE);
            int ClubID = spU.getClubID();
            Log.e("ClubID", String.valueOf(ClubID));
            params.put("club_id", String.valueOf(ClubID));
            t = HttpUtils.submitPostData("http://corp.xyser.com/Home/Api/getMemberList", params, "utf-8");
            Log.e("LoginAPI", t);
            savaCache(t);
        }
        try {
            jsonObj = new JSONObject(t);
            code = jsonObj.getInt("code");
        }catch (JSONException e){}

        if(code != 200){
            spU = new SharePreferenceUtil(LoginApi.USER_INFO_FILE);
            HashMap<String, String> params = new HashMap<String, String>();
            int ClubID = spU.getClubID();
            Log.e("ClubID", String.valueOf(ClubID));
            params.put("club_id", String.valueOf(ClubID));
            t = HttpUtils.submitPostData("http://corp.xyser.com/Home/Api/getMemberList", params, "utf-8");
            Log.e("LoginAPI", t);
            savaCache(t);
        }


        try {
            jsonObj = new JSONObject(t);
            code = jsonObj.getInt("code");
            String msg = jsonObj.getString("msg");
            if (code == 302) {
                // new AlertDialog.Builder(LoginActivity.loginactivity).setTitle(msg).setMessage("请重试").setPositiveButton("确定", null).show();
                return null;
            }

            if (code == 200) {
                try {
                    JSONArray data = jsonObj.getJSONArray("data");
                    String[][] s = new String[data.length()][23];
                    for(int i = 0; i < data.length() ; i++){
                        JSONObject jsoni = (JSONObject)data.get(i);
                        s[i][0] = jsoni.getString("id");
                        s[i][1] = jsoni.getString("corp_id");
                        s[i][2] = jsoni.getString("member_id");
                        s[i][3] = jsoni.getString("uid");
                        s[i][4] = jsoni.getString("cost");
                        s[i][5] = jsoni.getString("operator");
                        s[i][6] = jsoni.getString("name");
                        s[i][7] = jsoni.getString("sex");
                        s[i][8] = jsoni.getString("photo");
                        s[i][9] = jsoni.getString("phone");
                        s[i][10] = jsoni.getString("email");
                        s[i][11] = jsoni.getString("qq");
                        s[i][12] = jsoni.getString("school");
                        s[i][13] = jsoni.getString("college");
                        s[i][14] = jsoni.getString("major");
                        s[i][15] = jsoni.getString("self_evaluation");
                        s[i][16] = jsoni.getString("openid");
                    }
                    return s;
                } catch (JSONException e) {
                    System.out.println("Jsons parse error !");
                    e.printStackTrace();
                }

                return null;
            }
        }catch (JSONException e) {
            //new AlertDialog.Builder(LoginActivity.loginactivity).setTitle("服务器返回错误").setMessage("请稍后重试").setPositiveButton("确定",null).show();
            Log.e("LoginAPI", "返回数据解析错误" + e.getMessage());
            //login_ret = false;
            //e.printStackTrace();
            return null;
        }
        return null;
    }

    private static void saveMemberList(){

    }

    private static void savaCache(String cache){
        spU = new SharePreferenceUtil("cache_file");
        spU.setValue("member_list",cache);
    }

    private static String getCache(){
        spU = new SharePreferenceUtil("cache_file");
        return spU.getValue("member_list");
    }
}
